package com.aiju.cash.ui.member;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.MemberApi;
import com.aiju.cash.core.event.MemberEvent;
import com.aiju.cash.util.MyTextWatcher;
import com.aiju.common.event.EventCenter;
import com.aiju.utils.CheckUtil;
import com.aiju.utils.KeyBoardUtils;
import com.aiju.utils.T;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2017-02-20 15:59
 * @version: V1.0
 * @Description: 新建会员
 */
public class MemberAddActivity extends BaseTitleActivity {

    private EditText mEditText;
    private String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_add);
        setCenterTitle("新建会员");

        mEditText = (EditText) findViewById(R.id.ev_menber_phone);
        mEditText.addTextChangedListener(new MyTextWatcher(mEditText));
        findViewById(R.id.btn_member_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phone = mEditText.getText().toString().replace(" ", "");
                if (!CheckUtil.checkPhone(phone)) {
                    T.ToastShort("手机格式不正确");
                    return;
                }
                requestSaveMember();
            }
        });
    }

    /**
     * 添加会员信息
     */
    private void requestSaveMember() {
        mDialogHelper.showLoadingProgressDialog();
        new MemberApi().memberAdd(phone, mUser.getToken(), mUser.getStoreId(),
                "", phone, "", "", "", "", "", "", new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        mDialogHelper.dismissProgressDialog();
                        T.ToastNetError();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        mDialogHelper.dismissProgressDialog();
                        DataJson dataJson = new DataJson(response);
                        if (dataJson.isSuccess()) {
                            T.ToastShort("会员创建成功");
                            try {
                                //{"status":200,"msg":"操作成功","data":{"id":"42","type":"new"}}
                                JSONObject jsonObject = new JSONObject(dataJson.getData().toString());
                                String type = jsonObject.getString("type");
                                String ids = jsonObject.getString("id");
                                if (type.equals("new")) {
                                    EventCenter.getInstance().post(new MemberEvent());
                                    goMemberInfoActivity(ids);
                                    finish();
                                } else {
                                    goMemberInfo(ids);
                                    finish();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            T.ToastShort(dataJson.getMsg());
                        }
                    }
                });
    }

    /**
     * 会员详情
     */
    private void goMemberInfo(String memberId) {
        Intent intent = new Intent(this, MemberInfoCardsActivity.class);
        intent.putExtra("memberId", memberId);
        startActivity(intent);
    }

    /**
     * 跳转添加会员详情
     */
    private void goMemberInfoActivity(String memberId) {
        Intent intent = new Intent(this, MemberInfoActivity.class);
        intent.putExtra("type", MemberInfoActivity.ADD);
        intent.putExtra("phone", phone);
        intent.putExtra("memberId", memberId);
        startActivity(intent);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        KeyBoardUtils.closeKeybord(mEditText, this);
    }
}
